/*****************************************************************************
 * mysql.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/
#if 0
/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "mysql/mysql.h"
#include "dev_mysql.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL   LOG_LEVEL_DEBUG
#define LOG_MODULE      "dev.mysql"
#include "thirdparty/log/log.h" 
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/


PLUM_PRIVATE
plum_s32    dev_mysql_tb_show(dev_mysql_hdl_t hdl,
                            plum_void (cb)(plum_u32 argc,plum_s8 **argv))
{
    plum_s32 rc = PLUM_ECODE_OK;
    
    do{
        plum_s8     cmd[] = "show tables;";
        rc = mysql_real_query(hdl,cmd,strlen(cmd));
        if(rc){
            LOG_E("mysql_query err : %s",mysql_error(hdl));
            rc = PLUM_ECODE_EIO;
            break;
        }

        MYSQL_RES  *res = mysql_store_result(hdl);
        if(!res){
            LOG_E("mysql_store_result err: %s",mysql_error(hdl));
            break;
        }

        plum_u32 cnt = 1;
        while(1){
            MYSQL_ROW row = mysql_fetch_row(res);
            if(!row){
                LOG_D("mysql_fetch_row finish");
                break;
            }

            if(cb){
                cb(1,row);
            }
        }
    }while(0);

    return (rc);
}

PLUM_PRIVATE
plum_s32    dev_mysql_tb_select(dev_mysql_hdl_t hdl,plum_s8 *req,
                        plum_void (cb)(plum_u32 argc,plum_s8 **argv))
{
    plum_s32 rc = PLUM_ECODE_OK;
    
    do{
        rc = mysql_real_query(hdl,req,strlen(req));
        if(rc){
            LOG_E("mysql_query err : %s",mysql_error(hdl));
            rc = PLUM_ECODE_EIO;
            break;
        }

        MYSQL_RES  *res = mysql_store_result(hdl);
        if(!res){
            LOG_E("mysql_store_result err: %s",mysql_error(hdl));
            break;
        }

        plum_u32 obj = mysql_num_fields(res);

        while(1){
            MYSQL_ROW row = mysql_fetch_row(res);
            if(!row){
                LOG_D("mysql_fetch_row finish");
                break;
            }

            if(cb){
                cb(obj,row);
            }
        }
    }while(0);

    return (rc);
}

PLUM_PRIVATE
plum_s32    dev_mysql_tb_insert(dev_mysql_hdl_t hdl,plum_s8 *req)
{
    plum_s32 rc = PLUM_ECODE_OK;
    
    do{
        rc = mysql_real_query(hdl,req,strlen(req));
        if(rc){
            LOG_E("mysql_query err : %s",mysql_error(hdl));
            rc = PLUM_ECODE_EIO;
            break;
        }
    }while(0);

    return (rc);
}

PLUM_PRIVATE
plum_s32    dev_mysql_tb_update(dev_mysql_hdl_t hdl,plum_s8 *req)
{
    plum_s32 rc = PLUM_ECODE_OK;
    
    do{
        rc = mysql_real_query(hdl,req,strlen(req));
        if(rc){
            LOG_E("mysql_query err : %s",mysql_error(hdl));
            rc = PLUM_ECODE_EIO;
            break;
        }
    }while(0);

    return (rc);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

dev_mysql_hdl_t    dev_mysql_connect(plum_s8 *host,plum_u32 port,
                            plum_s8 *usr,plum_s8 *passwd,
                            plum_s8 *db)
{
    MYSQL   *mysql = NULL;

    do{
        if(!usr){
            LOG_E("please enter user name for connecting");
            break;
        }

        mysql = mysql_init(NULL);
        if(mysql == NULL){
            LOG_E("mysql_init err");
        }
        else{
            LOG_D("mysql init success : %p",mysql);
        }

        if(mysql_real_connect(mysql,host,usr,passwd,db,port,NULL,0) == NULL){
            LOG_E("mysql_real_connect err, %s",mysql_error(mysql));
        }
        LOG_I("mysql_real_connect");

        mysql_query(mysql, "set names utf8");
    }while(0);

    return (mysql);
}

plum_void     dev_mysql_discon(dev_mysql_hdl_t hdl)
{
    mysql_close(hdl);
}

plum_s32    dev_mysql_select(dev_mysql_hdl_t hdl,plum_s8 *cmd)
{
    plum_s32 rc = PLUM_ECODE_OK;
    
    do{
        rc = mysql_real_query(hdl,cmd,strlen(cmd));
        if(rc){
            LOG_E("mysql_query err : %s",mysql_error(hdl));
            rc = PLUM_ECODE_EIO;
            break;
        }

        MYSQL_RES  *res = mysql_store_result(hdl);
        if(!res){
            LOG_E("mysql_store_result err: %s",mysql_error(hdl));
            break;
        }

        while(1){
            MYSQL_ROW row = mysql_fetch_row(res);
            if(!row){
                LOG_D("mysql_fetch_row finish");
                break;
            }

            LOG_D("============table select==================");
            LOG_D("row[0] : %s",row[0]);
            LOG_D("row[1] : %s",row[1]);
            LOG_D("row[2] : %s",row[2]);
            LOG_D("row[3] : %s",row[3]);
            LOG_D("row[4] : %s",row[4]);
            LOG_D("row[5] : %s",row[5]);
        }
    }while(0);
}


/*****************************************************************************
 * Cli Functions
 ****************************************************************************/

plum_void   dev_cli_tb_select_cb(plum_u32 argc,plum_s8 **argv)
{
    plum_u32 loop;
    LOG_D("=============list=============");
    for(loop=0;loop<argc;loop++){
        LOG_D("%s",argv[loop]);
    }
}

plum_void   dev_cli_mysql_table_select(plum_u32 argc,plum_s8 **argv)
{
    dev_mysql_hdl_t hdl = strtoul(argv[0],0,16);
    char table[32] = {0};
    snprintf(table,sizeof(table),"%s",argv[1]);

    LOG_D("hdl : %p" ,hdl);
    LOG_D("table: %s",table);
    
    char cmd[64] = {0};
    snprintf(cmd,sizeof(cmd),"select * from %s;",table);
    dev_mysql_tb_select(hdl,cmd,dev_cli_tb_select_cb);
}
PLUM_CAL_CLI_EXPORT("tb_select","select mysql table",dev_cli_mysql_table_select);

plum_void   dev_cli_tb_show_cb(plum_u32 argc,plum_s8 **argv)
{
    LOG_D("table:   %s",argv[0]);
}

plum_void   dev_cli_mysql_table_show(plum_u32 argc,plum_s8 **argv)
{
    dev_mysql_hdl_t hdl = strtoul(argv[0],0,16);

    LOG_D("hdl : %p" ,hdl);

    dev_mysql_tb_show(hdl,dev_cli_tb_show_cb);
}
PLUM_CAL_CLI_EXPORT("tb_show","show mysql tables",dev_cli_mysql_table_show);



/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
#endif